<!-- ^2_3^(220105) -->
<template>
  <a-modal
    :title="title"
    :width="600"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="菜单ID"
        >
          <a-input name="id" v-decorator="['id', {}]" disabled />
        </a-form-item>

        <a-form-item
          label="父级PID"
        >
          <a-input name="pid" v-decorator="['pid', {}]" disabled />
        </a-form-item>

        <a-form-item
          label="菜单名称(title)"
        >
          <a-input name="title" v-decorator="['title', {}]" />
        </a-form-item>

        <a-form-item
          label="菜单标示(name)">
          <a-input name="name" v-decorator="['name', {}]" />
        </a-form-item>

        <a-form-item
          label="是否菜单节点">
          <a-radio-group v-decorator="['is_menu', { initialValue: 1 }]">
            <a-radio :value="0">否</a-radio>
            <a-radio :value="1">是</a-radio>
          </a-radio-group>
        </a-form-item>

        <a-form-item
          label="是否显示">
          <a-radio-group v-decorator="['status', { initialValue: 1 }]">
            <a-radio :value="0">隐藏</a-radio>
            <a-radio :value="1">显示</a-radio>
          </a-radio-group>
        </a-form-item>

        <a-form-item
          label="是否分组">
          <a-radio-group v-decorator="['group', { initialValue: 0 }]">
            <a-radio :value="0">否</a-radio>
            <a-radio :value="1">是</a-radio>
          </a-radio-group>
        </a-form-item>

        <a-form-item
          label="图标">
          <a-input name="icon" v-decorator="['icon', {}]" />
        </a-form-item>

        <a-form-item
          label="组件(component)">
          <a-input name="component" v-decorator="['component', {}]" />
        </a-form-item>

        <a-form-item
          label="跳转(redirect)">
          <a-input name="redirect" v-decorator="['redirect', {}]" />
        </a-form-item>

        <a-form-item
          label="路径(path)">
          <a-input name="path" v-decorator="['path', {}]" />
        </a-form-item>

        <a-form-item
          label="控制器">
          <a-input name="controller" v-decorator="['controller', {}]" />
        </a-form-item>

        <a-form-item
          label="操作">
          <a-input name="action" v-decorator="['action', {}]" />
        </a-form-item>

        <a-form-item
          label="是否隐藏页头">
          <a-radio-group v-decorator="['is_hide_header', { initialValue: 0 }]">
            <a-radio :value="0">否</a-radio>
            <a-radio :value="1">是</a-radio>
          </a-radio-group>
        </a-form-item>

        <a-form-item
          label="是否隐藏子级">
          <a-radio-group v-decorator="['is_hide_children', { initialValue: 0 }]">
            <a-radio :value="0">否</a-radio>
            <a-radio :value="1">是</a-radio>
          </a-radio-group>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { menuAdd, menuEdit } from '@/api/menu'

// ^2_3^
export default {
  name: 'MenuModal',
  data () {
    return {
      title: '菜单管理',
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      visible: false,
      confirmLoading: false,
      mdl: {}
    }
  },
  beforeCreate () {
    this.form = this.$form.createForm(this)
    console.log('>modal form::', this.form)
  },
  created () {

  },
  methods: {
    add (record) {
      this.edit(record)
    },
    edit (record) {
      this.mdl = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.resetFields()
        this.form.setFieldsValue({ ...record })
      })
    },
    close () {
      this.$emit('close')
      this.visible = false
    },
    handleOk () {
      const _this = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        // 验证表单没错误
        if (!err) {
          console.log('=>form values', values)

          _this.confirmLoading = true
          // 模拟后端请求 2000 毫秒延迟
          new Promise((resolve, reject) => {
            // setTimeout(() => resolve(), 2000)
            //
            if (values['id'] === 0) {
              // 新增
              menuAdd(values).then(response => {
                if (response && response.code === 0) {
                  resolve()
                } else {
                  _this.$message.error(response.code + ' ' + response.msg)
                  _this.confirmLoading = false
                  // reject(response.msg)
                }
              }).catch(error => {
                reject(error)
              })
            } else {
              // 编辑
              menuEdit(values).then(response => {
                if (response && response.code === 0) {
                  resolve()
                } else {
                  _this.$message.error(response.code + ' ' + response.msg)
                  _this.confirmLoading = false
                  // reject(response.msg)
                }
              }).catch(error => {
                reject(error)
              })
            }
          }).then(() => {
            _this.$message.success('保存成功')
            _this.$emit('ok')
          }).catch(() => {
          }).finally(() => {
            _this.confirmLoading = false
            _this.close()
          })
        }
      })
    },
    handleCancel () {
      this.close()
    }
  }
}
</script>
